package com.tj.template.application.product.output;

import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.tj.bo.common.conf.api.ResponseData;
import com.tj.bo.product.client.vo.MoneyLimit;
import com.tj.bo.product.client.vo.Product;
import com.tj.bo.product.client.vo.ProductListVo;
import com.tj.bo.product.client.vo.TimeRange;
import com.tj.bo.trade.client.enums.TradeTypeEnum;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: binary-option
 * @description
 * @author: binary
 * @create: 2020-06-12 10:36
 **/
public class ProductListResponse implements ResponseData {

    private List<ProductResponse> products;
    private List<TimeRange> chartTimeRange;
    private List<TimeRange> orderTimeRange;
    private Integer isAutoOrderSwitchOpen = 0;
    private MoneyLimit moneyLimit;
    private String coinType;
    private boolean openRecharge;

    public boolean isOpenRecharge() {
        return openRecharge;
    }

    public ProductListResponse setOpenRecharge(boolean openRecharge) {
        this.openRecharge = openRecharge;
        return this;
    }

    public List<TimeRange> getChartTimeRange() {
        return chartTimeRange;
    }

    public ProductListResponse setChartTimeRange(List<TimeRange> chartTimeRange) {
        this.chartTimeRange = chartTimeRange;
        return this;
    }

    public List<TimeRange> getOrderTimeRange() {
        return orderTimeRange;
    }

    public ProductListResponse setOrderTimeRange(List<TimeRange> orderTimeRange) {
        this.orderTimeRange = orderTimeRange;
        return this;
    }

    public Integer getIsAutoOrderSwitchOpen() {
        return isAutoOrderSwitchOpen;
    }

    public ProductListResponse setIsAutoOrderSwitchOpen(Integer isAutoOrderSwitchOpen) {
        this.isAutoOrderSwitchOpen = isAutoOrderSwitchOpen;
        return this;
    }

    public MoneyLimit getMoneyLimit() {
        return moneyLimit;
    }

    public ProductListResponse setMoneyLimit(MoneyLimit moneyLimit) {
        this.moneyLimit = moneyLimit;
        return this;
    }

    public String getCoinType() {
        return coinType;
    }

    public ProductListResponse setCoinType(String coinType) {
        this.coinType = coinType;
        return this;
    }

    public List<ProductResponse> getProducts() {
        return products;
    }

    public ProductListResponse setProducts(List<ProductResponse> products) {
        this.products = products;
        return this;
    }

    public ProductListResponse(ProductListVo productListVo, Integer tradeType) {
        List<ProductResponse> productList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(productListVo.getProducts())) {
            for (Product product : productListVo.getProducts()) {
                ProductResponse response = new ProductResponse();
                BeanUtils.copyProperties(product, response);
                response.setLocked(product.isLocked());
                response.setOdds(new BigDecimal(product.getOdds()).multiply(new BigDecimal(100)).intValue());
//                if(tradeType == TradeTypeEnum.VIRTUAL_TRADE.getCode()){
//                    response.setLocked(false);
//                }
                productList.add(response);
            }
        }
        this.products = productList.stream().sorted(Comparator.comparing(ProductResponse::getOdds).reversed()).collect(Collectors.toList())
                .stream().sorted(Comparator.comparing(ProductResponse::isLocked, (o1, o2) -> {
                    if (o1 ^ o2) {
                        return o1 ? 1 : -1;
                    } else {
                        return 0;
                    }
                })).collect(Collectors.toList());
    }
}
